$(document).ready(function () {
	
	$('#EmprestimoAlunoId').select2({
		allowClear: true, 
		formatNoMatches: function(term) { 
			return 'Nenhum aluno encontrado'; 
		}
	});
	$('#EmprestimoProjetoId').select2({allowClear: true});
	$('#EmprestimoAutorizadorId').select2({allowClear: true});
	$('#EmprestimoDataInicio').mask('99/99/9999').datepicker();
	$('#EmprestimoDataEntrega').mask('99/99/9999').datepicker();

	$('#Itensemprestimo0EquipamentoId').select2({
		minimumInputLength: 1,
	    allowClear: true,
	    formatSearching: function() { return 'Procurando...'; },
	    formatNoMatches: function(term) { return 'Nenhum equipamento encontrado'; },
	    formatInputTooShort: function(term, minLength1) { return 'Digite no mínimo um caractere'},
	    ajax: {
	    	url: baseUrl + 'equipamentos/autocomplete',
	      	dataType: 'json',
	      	data: function (term, page) {
	        	return {
	          		q: term,
	          		page_limit: 20
	        	};
	      	},
	      	results: function (data, page) {
	        	return { results: data };
	      	}
		}
	});

	$('#btn-add-solicitante').click(function(event){
		event.preventDefault();
		var url = $(this).attr('href');
		var title = $(this).attr('title');
		$("#ajax-indicator").show();
		$("#form-modal .modal-body").empty();
        $.get(url, function (data) {
        	$('#form-modal .modal-header h4:first').text(title);
        	$("#form-modal .modal-body").html(data);
        	$("#form-modal").modal();  
        })
        .always(function(){
        	$("#ajax-indicator").hide();
        });
	});

	$('#btn-save-solicitante').click(function(event){
		event.preventDefault();
		var button = $(this);
		var copyButton = button.clone(true);
		var content =  $("#modal-add-solicitante .modal-body");
		var form = content.children('form');
		
		$("#ajax-indicator").show();
		button.addClass('disabled').children('span').text('Salvando...').attr({disabled: 'disabled'});
        $.post(form.attr('action'), form.serialize(), function (response) {
            if ($(response).find('.error').length) {
            	content.html(response);
            	button.replaceWith(copyButton);
            } else {
                $('#EmprestimoAlunoId').append(new Option($(response).find('#AlunoNome').val(), $(response).find('#AlunoId').val()));
                $('#EmprestimoAlunoId').select2("val", $(response).find('#AlunoId').val());
                button.replaceWith(copyButton);
                $("#modal-add-solicitante").modal('hide');
            }
        })
        .always(function(){
        	$("#ajax-indicator").hide();
        });
	});

	$('#btn-add-projeto').click(function(event){
		event.preventDefault();
		var modal = $("#modal-add-projeto");
		var content =  $("#modal-add-projeto .modal-body");
		
		$("#ajax-indicator").show();
		content.empty();
        $.get($(this).attr('href'), function (data) {
            content.html(data);
            modal.modal();  
        })
        .always(function(){
        	$("#ajax-indicator").hide();
        });
	});

	$('#btn-save-projeto').click(function(event){
		event.preventDefault();
		var button = $(this);
		var copyButton = button.clone(true);
		var content =  $("#modal-add-projeto .modal-body");
		var form = content.children('form');
		
		$("#ajax-indicator").show();
		button.addClass('disabled').children('span').text('Salvando...').attr({disabled: 'disabled'});
        $.post(form.attr('action'), form.serialize(), function (response) {
            if ($(response).find('.error').length) {
            	content.html(response);
            	button.replaceWith(copyButton);
            } else {
                $('#EmprestimoProjetoId').append(new Option($(response).find('#ProjetoNome').val(), $(response).find('#ProjetoId').val()));
                $('#EmprestimoProjetoId').select2("val", $(response).find('#ProjetoId').val());
                button.replaceWith(copyButton);
                $("#modal-add-projeto").modal('hide');
            }
        })
        .always(function(){
        	$("#ajax-indicator").hide();
        });
	});

	$('#btn-search-equipamento').click(function(event){
		event.preventDefault();
		var url = $(this).attr('href');
		var title = $(this).attr('title');
		$("#ajax-indicator").show();
		$("#form-modal .modal-body").empty();
        $.get(url, function (data) {
        	$('#form-modal .modal-header h4:first').text(title);
        	$("#form-modal .modal-body").html(data);
        	$("#form-modal").modal();  
        })
        .always(function(){
        	$("#ajax-indicator").hide();
        });
	});

	});